package com.github.jspxnet.worktask.action;

import com.github.jspxnet.boot.res.LanguageRes;
import com.github.jspxnet.enums.ErrorEnumType;
import com.github.jspxnet.sioc.annotation.Bean;
import com.github.jspxnet.txweb.AssertException;
import com.github.jspxnet.txweb.IUserSession;
import com.github.jspxnet.txweb.annotation.Describe;
import com.github.jspxnet.txweb.annotation.HttpMethod;
import com.github.jspxnet.txweb.annotation.Operate;
import com.github.jspxnet.txweb.annotation.Param;
import com.github.jspxnet.txweb.result.RocResponse;
import com.github.jspxnet.utils.BeanUtil;
import com.github.jspxnet.utils.StringUtil;
import com.github.jspxnet.worktask.env.JWorkTask;
import com.github.jspxnet.worktask.model.param.ConfFlowNodeParam;
import com.github.jspxnet.worktask.model.param.ConfWorkFlowParam;
import com.github.jspxnet.worktask.table.ConfFlowJob;
import com.github.jspxnet.worktask.table.ConfFlowNode;
import com.github.jspxnet.worktask.table.ConfWorkFlow;
import com.github.jspxnet.worktask.view.ConfFlowView;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jspx.net
 * author: chenYuan
 * date: 2021/9/28 0:16
 * description: jspx-framework
 * jworktask/config
 **/
@HttpMethod(caption = "流程配置管理", actionName = "*", namespace = JWorkTask.namespace + "/config")
@Bean(namespace = JWorkTask.namespace, singleton = true)
public class ConfFlowAction extends ConfFlowView {


    @Operate(caption = "添加流程配置", method = "save")
    @Describe
    public RocResponse<Integer> save(@Param(caption = "节点参数") ConfWorkFlowParam params) throws Exception {
        ConfWorkFlow confWorkFlow = BeanUtil.copy(params, ConfWorkFlow.class);
        AssertException.isNull(confWorkFlow.getFlowNo(), "流程编号不能为空");
        AssertException.isNull(confWorkFlow.getNamespace(), "命名空间不能为空");
        AssertException.isNull(confWorkFlow.getTitle(), "标题不能为空");
        IUserSession userSession = getUserSession();
        confWorkFlow.setPutUid(userSession.getUid());
        confWorkFlow.setPutName(userSession.getName());

        List<ConfFlowNode> nodeList = new ArrayList<>();
        for (ConfFlowNodeParam confFlowNode:params.getNodeList())
        {
            ConfFlowNode node = BeanUtil.copy(confFlowNode,ConfFlowNode.class);
            ConfFlowJob confFlowJob = BeanUtil.copy(confFlowNode.getConfFlowJob(), ConfFlowJob.class);
            if (!StringUtil.isNull(confFlowJob.getNodeId()) && !StringUtil.isNull(confFlowJob.getFlowNo()))
            {
                node.setConfFlowJob(confFlowJob);
            } else
            {
                node.setConfFlowJob(null);
            }
            nodeList.add(node);
        }
        confWorkFlow.setNodeList(nodeList);
        if (confWorkFlowDAO.save(confWorkFlow,true) > 0)
        {
            return RocResponse.success(1, language.getLang(LanguageRes.saveSuccess));
        }
        return RocResponse.error(ErrorEnumType.DATABASE.getValue(), language.getLang(LanguageRes.saveFailure));
    }


    @Operate(caption = "删除流程", method = "delete")
    public RocResponse<Integer> delete(@Param(caption = "ID列表", required = true) Long[] ids) {
        return RocResponse.success(confWorkFlowDAO.delete(ConfWorkFlow.class, ids, true), language.getLang(LanguageRes.deleteSuccess));
    }

    @Operate(caption = "更新节点", method = "node/update")
    public RocResponse<Long> nodeUpdate(@Param(caption = "ID列表", required = true) ConfFlowNodeParam param)
    {
        AssertException.isTrue(param.getId()<=0,"节点ID不能为空");
        ConfFlowNode node = BeanUtil.copy(param,ConfFlowNode.class);
        ConfFlowJob confFlowJob = BeanUtil.copy(param.getConfFlowJob(), ConfFlowJob.class);
        if (!StringUtil.isNull(confFlowJob.getNodeId()) && !StringUtil.isNull(confFlowJob.getFlowNo()))
        {
            node.setConfFlowJob(confFlowJob);
        } else
        {
            node.setConfFlowJob(null);
        }
        confWorkFlowDAO.delete(ConfFlowNode.class,param.getId());
        if (confFlowJob.getId()>0)
        {
            confWorkFlowDAO.delete(ConfFlowJob.class,confFlowJob.getId());
        }
        try {
            confWorkFlowDAO.save(node);
            if (!StringUtil.isNull(confFlowJob.getNodeId()) && !StringUtil.isNull(confFlowJob.getFlowNo()))
            {
                confWorkFlowDAO.save(confFlowJob);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RocResponse.success(node.getId(), language.getLang(LanguageRes.deleteSuccess));
    }

    @Operate(caption = "删除流程", method = "node/delete")
    public RocResponse<Integer> nodeDelete(@Param(caption = "ID列表", required = true) Long[] ids) {

        confWorkFlowDAO.delete(ConfFlowNode.class,ids,true);
        return RocResponse.success(confWorkFlowDAO.delete(ConfWorkFlow.class, ids, true), language.getLang(LanguageRes.deleteSuccess));
    }

    @Override
    public String execute() throws Exception {
        if (isMethodInvoked()) {
            confWorkFlowDAO.evict(ConfWorkFlow.class);
            confWorkFlowDAO.evict(ConfFlowNode.class);
            confWorkFlowDAO.evict(ConfFlowJob.class);
        }
        return super.execute();
    }

}
